package com.i2works.smartluolong.manage.controller.admin;


import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.i2works.smartluolong.manage.entity.admin.AdminMenuDO;
import com.i2works.smartluolong.manage.entity.admin.AdminMenuQuery;
import com.i2works.smartluolong.manage.service.admin.IAdminMenuService;
import com.i2works.smartluolong.manage.utils.BaseModule;
import com.i2works.smartluolong.utils.base.URLConstant;
import com.i2works.smartluolong.utils.base.MsgEnum;
import com.i2works.smartluolong.utils.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 系统菜单
 *
 * @author koal <koal@vip.qq.com>
 * @ClassName: AdminMenuController
 * @date 2016年01月21日 下午10:48:29
 */
@Controller
@RequestMapping("/adminMenu")
public class AdminMenuController extends BaseModule {


    private final IAdminMenuService adminMenuService;

    @Autowired
    public AdminMenuController(IAdminMenuService adminMenuService) {
        this.adminMenuService = adminMenuService;
    }

    /**
     * 系统菜单管理页面
     */
    @GetMapping(value = "/view/index")
    public String index() {
        logger.info("系统菜单管理页面");
        return URLConstant.Admin.MENU_URL;
    }


    /**
     * 菜单列表
     */
    @GetMapping(value = "/view/api")
    @ResponseBody
    public Result queryList(@ModelAttribute AdminMenuQuery query) {
        Result result = new Result();
        try {
            int total = adminMenuService.queryCount(query);
            List<AdminMenuDO> queryAdminMenuList = Lists.newArrayList();
            if (total > 0) {
                queryAdminMenuList = adminMenuService.queryList(query);
            }
            result.setTotal(total);
            result.setRows(queryAdminMenuList);
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.SYSTEM_ERROR.getMsg());
        }
        return result;
    }

    /**
     * 查询分类菜单树（只显示类型是1的目录菜单）
     */
    @GetMapping(value = "/view/classTree")
    @ResponseBody
    public Result queryClassTree() {
        Result result = new Result();
        try {
            AdminMenuQuery query = new AdminMenuQuery();
            query.setMenuType(1);
            List<AdminMenuDO> queryAdminMenuList = adminMenuService.queryAllList(query);
            result.setRows(queryAdminMenuList);
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.SYSTEM_ERROR.getMsg());
        }
        return result;
    }


    /**
     * 新增编辑系统菜单跳转
     */
    @GetMapping(value = "/save/form")
    public String saveForm() {
        logger.info("系统菜单新增编辑");
        return URLConstant.Admin.MENU_FORM_URL;
    }


    /**
     * 添加系统菜单
     */
    @PostMapping(value = "/save/action")
    @ResponseBody
    public Result save(@ModelAttribute AdminMenuDO adminMenu) {
        Result result = new Result();
        try {
            if (adminMenu.getPid() == null) {
                adminMenu.setPid(0);
            }
            adminMenu.setUpdateName(getUserName());
            adminMenuService.save(adminMenu);
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.SAVE_ERROR.getMsg());
        }
        return result;
    }

    /**
     * 查询某个菜单
     */
    @PostMapping(value = "/view/{id}")
    @ResponseBody
    public Result query(@PathVariable Integer id) {
        Result result = new Result();
        try {
            AdminMenuQuery query = new AdminMenuQuery();
            query.setId(id);
            AdminMenuDO adminMenu = adminMenuService.query(query);
            result.setModel(adminMenu);
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.FIND_ERROR.getMsg());
        }
        return result;
    }


    /**
     * 某个系统菜单（名称、标示、代码）是否存在验证
     */
    @PostMapping(value = "/view/check")
    @ResponseBody
    public boolean queryCheck(@ModelAttribute AdminMenuQuery query) {
        boolean result = false;
        try {
            int count = adminMenuService.queryCheck(query);
            if (count <= 0) {
                result = true;
            }
        } catch (Exception e) {
            logger.error("", e);
        }
        return result;
    }

    /**
     * 更新菜单状态
     */
    @PostMapping(value = "/status/action")
    @ResponseBody
    public Result updateStatus(@ModelAttribute AdminMenuDO adminMenu) {
        Result result = new Result();
        try {
            adminMenu.setUpdateName(getUserName());
            adminMenuService.updateStatus(adminMenu);
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.STATUS_ERROR.getMsg());
        }
        return result;
    }

    @PostMapping(value = "/status/batchAction")
    @ResponseBody
    public Result updateBatchStatus(@RequestParam("idsList[]") List<Integer> idsList, Integer status) {
        Result result = new Result();
        try {
            if (idsList != null && idsList.size() > 0) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("idsList", idsList);
                map.put("status", status);
                map.put("updateName", getUserName());
                adminMenuService.updateBatchStatus(map);
            }
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.STATUS_ERROR.getMsg());
        }
        return result;
    }

    /**
     * 删除
     */
    @PostMapping(value = "/delete/action")
    @ResponseBody
    public Result delete(@RequestParam Integer id) {
        Result result = new Result();
        try {
            adminMenuService.delete(id);
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.DELETE_ERROR.getMsg());
        }
        return result;
    }

    /**
     * 批量删除
     */
    @PostMapping(value = "/delete/batchAction")
    @ResponseBody
    public Result deleteBatch(@RequestParam("idsList[]") List<Integer> idsList) {
        Result result = new Result();
        try {
            if (idsList != null && idsList.size() > 0) {
                adminMenuService.deleteBatch(idsList);
            }
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.DELETE_ERROR.getMsg());
        }
        return result;
    }
}
